package com.dmzc.stack;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * 9.匹配不适合的括号,可以加一些优化
 */
public class ValidParentheses {
    public static void main(String[] args) {
        boolean isTrue = true;
        String s = "{]";
        Map<String, String> map = new HashMap<>();
        Stack<String> stack = new Stack<>();
        map.put(")", "(");
        map.put("}", "{");
        map.put("]", "[");
        for (int i = 0; i < s.length(); i++) {
            String charStr = "" + s.charAt(i);
            if (map.containsKey(charStr)) {
                if (!stack.isEmpty()&&map.get(charStr).equals(stack.peek())) {
                    stack.pop();
                } else {
                    isTrue = false;
                    break;
                }
            } else {
                stack.push(charStr);
            }
        }
        if (!stack.empty())
            isTrue = false;
        System.out.println(isTrue);
    }
}
